
/*
    Copyright 2011,2012 Calum Stuart Cookson.

    This file is part of Bio Mongoose.

    Bio Mongoose is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Bio Mongoose is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Bio Mongoose.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "alphabet.h"
#include <iostream>
#include <iterator>

using namespace std;

alphabet::alphabet():alphabet_size(0){}
alphabet::alphabet(string sequence){extract_from_string(sequence);}
char alphabet::get_letter_from_index(int index) const
{
    return index_to_letter.at(index);
}
int alphabet::get_index_from_letter(char letter) const
{

    return letter_to_index.at(letter);
}
int alphabet::add_letter(char letter)
{
    int letter_index;
    if(!this->contains(letter))
    {
        letter_index = this->alphabet_size;
        letter_to_index[letter] = letter_index;
        index_to_letter[letter_index] = letter;
        alphabet_set.insert(string()+letter);
        alphabet_size++;
    }
    else letter_index = letter_to_index[letter];

    return letter_index;
}
bool alphabet::contains(char letter) const
{
    return (letter_to_index.find(letter)!=letter_to_index.end());
}
unsigned int alphabet::size() const
{
    return alphabet_size;
}
void alphabet::clear()
{
    letter_to_index.clear();
    index_to_letter.clear();
    alphabet_size = 0;
}
void alphabet::extract_from_string(string sequence)
{
    this->clear();
    for(string::size_type position = 0 ; position != sequence.length() ; position++)
    {
        this->add_letter(sequence.at(position));
    }
}

set<string> alphabet::get_set()
{
    return alphabet_set;
}

ostream& alphabet::report(ostream& out)
{
    out << "ALPHABET< ";

    for(map<char,int>::iterator iter = letter_to_index.begin(); iter != letter_to_index.end(); iter++)
    {
        out << iter->first << " ";
    }
    out << ">" << endl;
    return out;
}
